/*
 * Copyright 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.orkut.androidexample;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.orkut.client.api.DefaultOrkutAdapter;
import com.google.orkut.client.api.OrkutAdapterException;
import com.google.orkut.orkroid.Orkroid;
import com.google.orkut.orkroid.OrkroidAuthActivity;

public class WelcomeActivity extends Activity implements View.OnClickListener {
	protected Button proceedButton;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        proceedButton = (Button) findViewById(R.id.proceedbutton);
        proceedButton.setOnClickListener(this);
        
        // if the "logout" parameter was passed in the Intent,
        // we have to erase the existing credentials
        if (getIntent().getBooleanExtra("logout", false)) {
        	SharedPreferences.Editor spe = getPreferences(MODE_PRIVATE).edit();
        	spe.putString("accesspass", "");
        	spe.commit();
        }
    }

    public void onClick(View target) {
    	if (target != proceedButton) return;
    	
    	try {
    		String consumerKey = getResources().getString(R.string.consumerkey);
    		String consumerSecret = getResources().getString(R.string.consumersecret);
    	
    		Orkroid.init(new DefaultOrkutAdapter(consumerKey, consumerSecret, "about:blank", false, null));
    		
    		SharedPreferences sp = getPreferences(MODE_PRIVATE);
    		String accessPass = sp.getString("accesspass", "");
    		if (accessPass.equals("")) {
    			Intent intent = new Intent(this, OrkroidAuthActivity.class);
    			startActivityForResult(intent, 123);
    		}
    		else {
    			// we already have an access pass
    			Orkroid.getOA().setAccessPass(accessPass);
    			goToMainMenu();
    		}
    	}
    	catch (OrkutAdapterException ex) {
    		Toast.makeText(this, "Error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
    	}
    }

    protected void goToMainMenu() {
		Intent i = new Intent(this, MainMenuActivity.class);
		startActivity(i);
		finish();
    }

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			Toast.makeText(this,"Authentication successful.",Toast.LENGTH_LONG).show();
			SharedPreferences sp = getPreferences(MODE_PRIVATE);
			SharedPreferences.Editor spe = sp.edit();
			spe.putString("accesspass", Orkroid.getOA().getAccessPass());
			spe.commit();
			goToMainMenu();
		}
		else {
			Exception ex = (Exception) data.getSerializableExtra("exception");
			Toast.makeText(this,"Auth failed: " + ex.getMessage(), Toast.LENGTH_LONG).show();
		}
	}
}
